package ivan.cheng.tuqu.graphics;

import ivan.cheng.tuqu.util.BitmapUtil;
import android.graphics.Bitmap;
import android.graphics.Color;

public class ImageFilter {
	//private Bitmap image;

	private int iw, ih;

	private int[] pixels;

	public static float colormatrix_lomo[] = {
		    1.5f,  0.1f, 0.1f, 0, -163.1f,
		    0,  1.5f, 0.1f, 0, -163.1f,
		    0,  0.1f, 1.4f, 0, -163.1f,
		    0,  0, 0, 1.0f, 0 };

		//黑白
	public static float colormatrix_heibai[] = {
		    0.6f,  0.9f, 0.2f, 0, -173.9f,
		    0.6f,  0.9f, 0.2f, 0, -173.9f,
		    0.6f,  0.9f, 0.2f, 0, -173.9f,
		    0,  0, 0, 1.0f, 0 };

		//哥特
	public static float colormatrix_gete[] = {
		    1.9f,-0.3f, -0.2f, 0,-87.0f,
		    -0.2f, 1.7f, -0.1f, 0, -87.0f,
		    -0.1f,-0.6f, 2.0f, 0, -87.0f,
		    0, 0, 0, 1.0f, 0 };

		//淡雅
	public static float colormatrix_danya[] = {
		    0.6f,0.3f, 0.1f, 0,53.3f,
		    0.2f,0.7f, 0.1f, 0,53.3f,
		    0.2f,0.3f, 0.4f, 0,53.3f,
		    0, 0, 0, 1.0f, 0 };

		//酒红
	public static float colormatrix_jiuhong[] = {
		    1.2f,0.0f, 0.0f, 0.0f,0.0f,
		    0.0f,0.9f, 0.0f, 0.0f,0.0f,
		    0.0f,0.0f, 0.8f, 0.0f,0.0f,
		    0, 0, 0, 1.0f, 0 };

		//清宁
	public static float colormatrix_qingning[] = {
		    0.9f, 0, 0, 0, 0,
		    0, 1.1f,0, 0, 0,
		    0, 0, 0.9f, 0, 0,
		    0, 0, 0, 1.0f, 0 };

		//光晕
	public static float colormatrix_guangyun[] = {
		    0.9f, 0, 0,  0, 54.9f,
		    0, 0.9f,0,  0, 54.9f,
		    0, 0, 0.9f,  0, 54.9f,
		    0, 0, 0, 1.0f, 0 };

		//蓝调
	public static float colormatrix_landiao[] = {
		    2.1f, -1.4f, 0.6f, 0.0f, -21.0f,
		    -0.3f, 2.0f, -0.3f, 0.0f, -21.0f,
		    -1.1f, -0.2f, 2.6f, 0.0f, -21.0f,
		    0.0f, 0.0f, 0.0f, 1.0f, 0.0f
		};

		//梦幻
	public static float colormatrix_menghuan[] = {
		    0.8f, 0.3f, 0.1f, 0.0f, 40.5f,
		    0.1f, 0.9f, 0.0f, 0.0f, 40.5f,
		    0.1f, 0.3f, 0.7f, 0.0f, 40.5f,
		    0.0f, 0.0f, 0.0f, 1.0f, 0.0f
		};

		//夜色
	public static float colormatrix_yese[] = {
		    1.0f, 0.0f, 0.0f, 0.0f, -66.6f,
		    0.0f, 1.1f, 0.0f, 0.0f, -66.6f,
		    0.0f, 0.0f, 1.0f, 0.0f, -66.6f,
		    0.0f, 0.0f, 0.0f, 1.0f, 0.0f
		};
	
	public ImageFilter(Bitmap image) {
//		this.image = image;
		iw = image.getWidth();
		ih = image.getHeight();
		pixels = new int[iw * ih];
		image.getPixels(pixels, 0, iw, 0, 0, iw, ih);
	}

	public ImageFilter(int[] pixels2, int width, int height) {
		iw = width;
		ih = height;
		pixels = pixels2;
	}

	/** 图像二值化 */
	public Bitmap changeGrey() {
		// 设定二值化的域值，默认值为100
		int grey = 100;
		// 对图像进行二值化处理，Alpha值保持不变
		// ColorModel cm = ColorModel.getRGBdefault();
		for (int i = 0; i < iw * ih; i++) {
			int red, green, blue;
			int alpha = Color.alpha(pixels[i]);
			if (Color.red(pixels[i]) > grey) {
				red = 255;
			} else {
				red = 0;
			}

			if (Color.green(pixels[i]) > grey) {
				green = 255;
			} else {
				green = 0;
			}

			if (Color.blue(pixels[i]) > grey) {
				blue = 255;
			} else {
				blue = 0;
			}

			pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
		}
		// 将数组中的象素产生一个图像
		return BitmapUtil.instance().createBitmap(pixels, 0, iw, iw, ih,
				Bitmap.Config.RGB_565);
	}

	/** 提升清晰度,进行锐化 */
	public Bitmap sharp() {
		// 象素的中间变量
		int tempPixels[] = new int[iw * ih];
		for (int i = 0; i < iw * ih; i++) {
			tempPixels[i] = pixels[i];
		}
		// 对图像进行尖锐化处理，Alpha值保持不变
		for (int i = 1; i < ih - 1; i++) {
			for (int j = 1; j < iw - 1; j++) {
				int alpha = Color.alpha(pixels[i * iw + j]);

				// 对图像进行尖锐化
				int red6 = Color.red(pixels[i * iw + j + 1]);
				int red5 = Color.red(pixels[i * iw + j]);
				int red8 = Color.red(pixels[(i + 1) * iw + j]);
				int sharpRed = Math.abs(red6 - red5) + Math.abs(red8 - red5);

				int green5 = Color.green(pixels[i * iw + j]);
				int green6 = Color.green(pixels[i * iw + j + 1]);
				int green8 = Color.green(pixels[(i + 1) * iw + j]);
				int sharpGreen = Math.abs(green6 - green5)
						+ Math.abs(green8 - green5);

				int blue5 = Color.blue(pixels[i * iw + j]);
				int blue6 = Color.blue(pixels[i * iw + j + 1]);
				int blue8 = Color.blue(pixels[(i + 1) * iw + j]);
				int sharpBlue = Math.abs(blue6 - blue5)
						+ Math.abs(blue8 - blue5);

				if (sharpRed > 255) {
					sharpRed = 255;
				}
				if (sharpGreen > 255) {
					sharpGreen = 255;
				}
				if (sharpBlue > 255) {
					sharpBlue = 255;
				}

				tempPixels[i * iw + j] = alpha << 24 | sharpRed << 16
						| sharpGreen << 8 | sharpBlue;
			}
		}

		// 将数组中的象素产生一个图像
		return BitmapUtil.instance().createBitmap(pixels, 0, iw, iw, ih,
				Bitmap.Config.RGB_565);
	}

	/** 中值滤波 */
	public Bitmap median() {

		// 对图像进行中值滤波，Alpha值保持不变
		for (int i = 1; i < ih - 1; i++) {
			for (int j = 1; j < iw - 1; j++) {
				int red, green, blue;
				int alpha = Color.alpha(pixels[i * iw + j]);

				// int red2 = cm.getRed(pixels[(i - 1) * iw + j]);
				int red4 = Color.red(pixels[i * iw + j - 1]);
				int red5 = Color.red(pixels[i * iw + j]);
				int red6 = Color.red(pixels[i * iw + j + 1]);
				// int red8 = cm.getRed(pixels[(i + 1) * iw + j]);

				// 水平方向进行中值滤波
				if (red4 >= red5) {
					if (red5 >= red6) {
						red = red5;
					} else {
						if (red4 >= red6) {
							red = red6;
						} else {
							red = red4;
						}
					}
				} else {
					if (red4 > red6) {
						red = red4;
					} else {
						if (red5 > red6) {
							red = red6;
						} else {
							red = red5;
						}
					}
				}

				// int green2 = cm.getGreen(pixels[(i - 1) * iw + j]);
				int green4 = Color.green(pixels[i * iw + j - 1]);
				int green5 = Color.green(pixels[i * iw + j]);
				int green6 = Color.green(pixels[i * iw + j + 1]);
				// int green8 = cm.getGreen(pixels[(i + 1) * iw + j]);

				// 水平方向进行中值滤波
				if (green4 >= green5) {
					if (green5 >= green6) {
						green = green5;
					} else {
						if (green4 >= green6) {
							green = green6;
						} else {
							green = green4;
						}
					}
				} else {
					if (green4 > green6) {
						green = green4;
					} else {
						if (green5 > green6) {
							green = green6;
						} else {
							green = green5;
						}
					}
				}

				// int blue2 = cm.getBlue(pixels[(i - 1) * iw + j]);
				int blue4 = Color.blue(pixels[i * iw + j - 1]);
				int blue5 = Color.blue(pixels[i * iw + j]);
				int blue6 = Color.blue(pixels[i * iw + j + 1]);
				// int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]);

				// 水平方向进行中值滤波
				if (blue4 >= blue5) {
					if (blue5 >= blue6) {
						blue = blue5;
					} else {
						if (blue4 >= blue6) {
							blue = blue6;
						} else {
							blue = blue4;
						}
					}
				} else {
					if (blue4 > blue6) {
						blue = blue4;
					} else {
						if (blue5 > blue6) {
							blue = blue6;
						} else {
							blue = blue5;
						}
					}
				}
				pixels[i * iw + j] = alpha << 24 | red << 16 | green << 8
						| blue;
			}
		}

		// 将数组中的象素产生一个图像
		return BitmapUtil.instance().createBitmap(pixels, 0, iw, iw, ih,
				Bitmap.Config.RGB_565);
	}

	/** 线性灰度变换 */
	public Bitmap lineGrey() {

		// 对图像进行进行线性拉伸，Alpha值保持不变
		for (int i = 0; i < iw * ih; i++) {
			int alpha = Color.alpha(pixels[i]);
			int red = Color.red(pixels[i]);
			int green = Color.green(pixels[i]);
			int blue = Color.blue(pixels[i]);

			// 增加了图像的亮度
			red = (int) (1.1 * red + 30);
			green = (int) (1.1 * green + 30);
			blue = (int) (1.1 * blue + 30);
			if (red >= 255) {
				red = 255;
			}
			if (green >= 255) {
				green = 255;
			}
			if (blue >= 255) {
				blue = 255;
			}
			pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
		}

		// 将数组中的象素产生一个图像
		
		return BitmapUtil.instance().createBitmap(pixels, 0, iw, iw, ih,
				Bitmap.Config.RGB_565);
	}

	/** 转换为黑白灰度图 */
	public Bitmap grayFilter() {
		for (int i = 0; i < iw * ih; i++) {
			int alpha = Color.alpha(pixels[i]);
			int r = Color.red(pixels[i]);
			int g = Color.green(pixels[i]);
			int b = Color.blue(pixels[i]);

			r = g = b = (int)(0.30*r+0.59*g+0.11*b);
			
//			int index = i;

//			int r = (pixels[index] >> 16) & 0xff;
//
//			int g = (pixels[index] >> 8) & 0xff;
//
//			int b = pixels[index] & 0xff;

			    // 图像黑白化

//			R = r * 7 / 10;
//
//			R = (R < 0) ? 0 : ((R > 255) ? 255 : R);
//
//			G = g * 2 / 10;
//
//			G = (G < 0) ? 0 : ((G > 255) ? 255 : G);
//
//			B = b / 10;
//
//			B = (B < 0) ? 0 : ((B > 255) ? 255 : B);

			pixels[i] = Color.argb(alpha, r, g, b);


		}

		// 将数组中的象素产生一个图像
		
		return BitmapUtil.instance().createBitmap(pixels, 0, iw, iw, ih,
				Bitmap.Config.RGB_565);
	}
	
	private int changeRGBA(int red,int green,int blue,int alpha, float f[])//修改RGB的值
	{
	    int redV = red;
	    int greenV = green;
	    int blueV = blue;
	    int alphaV = alpha;
	    
	    red = (int) (f[0] * redV + f[1] * greenV + f[2] * blueV + f[3] * alphaV + f[4]);
	    green = (int) (f[0+5] * redV + f[1+5] * greenV + f[2+5] * blueV + f[3+5] * alphaV + f[4+5]);
	    blue = (int) (f[0+5*2] * redV + f[1+5*2] * greenV + f[2+5*2] * blueV + f[3+5*2] * alphaV + f[4+5*2]);
	    alpha = (int) (f[0+5*3] * redV + f[1+5*3] * greenV + f[2+5*3] * blueV + f[3+5*3] * alphaV + f[4+5*3]);
	    
	    if (red > 255)
	    {
	        red = 255;
	    }
	    if(red < 0)
	    {
	        red = 0;
	    }
	    if (green > 255)
	    {
	        green = 255;
	    }
	    if (green < 0)
	    {
	        green = 0;
	    }
	    if (blue > 255)
	    {
	        blue = 255;
	    }
	    if (blue < 0)
	    {
	        blue = 0;
	    }
	    if (alpha > 255)
	    {
	        alpha = 255;
	    }
	    if (alpha < 0)
	    {
	        alpha = 0;
	    }
	    return Color.argb(alpha, red, green, blue);
	}
	
	/** 转换为黑白灰度图 */
	public Bitmap iosFilter(float f[]) 
	{
		for (int i = 0; i < iw * ih; i++) {
			int alpha = Color.alpha(pixels[i]);
			int r = Color.red(pixels[i]);
			int g = Color.green(pixels[i]);
			int b = Color.blue(pixels[i]);
			pixels[i] = changeRGBA(r, g, b, alpha, f);
			//r = g = b = (int)(0.30*r+0.59*g+0.11*b);
			//pixels[i] = Color.argb(alpha, r, g, b);
		}

		// 将数组中的象素产生一个图像
		
		return BitmapUtil.instance().createBitmap(pixels, 0, iw, iw, ih,
				Bitmap.Config.RGB_565);
	}
	
//	Bitmap imageWithImage(Bitmap inImage,float f[])
//	{
//		unsigned char *imgPixel = RequestImagePixelData(inImage);
//		CGImageRef inImageRef = [inImage CGImage];
//		GLuint w = CGImageGetWidth(inImageRef);
//		GLuint h = CGImageGetHeight(inImageRef);
//		
//		int wOff = 0;
//		int pixOff = 0;
//		
//	    
//		for(GLuint y = 0;y< h;y++)//双层循环按照长宽的像素个数迭代每个像素点
//		{
//			pixOff = wOff;
//			
//			for (GLuint x = 0; x<w; x++)
//			{
//				
//				int red = (unsigned char)imgPixel[pixOff];
//				int green = (unsigned char)imgPixel[pixOff+1];
//				int blue = (unsigned char)imgPixel[pixOff+2];
//	            int alpha = (unsigned char)imgPixel[pixOff+3];
//	            changeRGBA(&red, &green, &blue, &alpha, f);
//	            
//	            //回写数据
//				imgPixel[pixOff] = red;
//				imgPixel[pixOff+1] = green;
//				imgPixel[pixOff+2] = blue;
//	            imgPixel[pixOff+3] = alpha;
//	            
//	            
//				pixOff += 4; //将数组的索引指向下四个元素
//			}
//	        
//			wOff += w * 4;
//		}
//	    
//		NSInteger dataLength = w * h * 4;
//	    
//	    //下面的代码创建要输出的图像的相关参数
//		CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
//	    
//		int bitsPerComponent = 8;
//		int bitsPerPixel = 32;
//		int bytesPerRow = 4 * w;
//		CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
//		CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
//		CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
//		
//		
//		CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow,colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);//创建要输出的图像
//		
//		UIImage *myImage = [UIImage imageWithCGImage:imageRef];
//		
//		CFRelease(imageRef);
//		CGColorSpaceRelease(colorSpaceRef);
//		CGDataProviderRelease(provider);
//		return myImage;
//	}
	
}